package models;

import java.util.Observable;

import constants.MessageType;

import logic.Motion;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.util.Pair;

/**
 * 
 * @author Merete, Mirna, Esben, Knut og Stian
 * 
 */
public abstract class PhysicalObject extends Observable{

	private static String TAG = PhysicalObject.class.getSimpleName();

	protected Bitmap bitmap;
	protected int spriteWidth;
	protected int spriteHeight;
	protected Motion motion;
	protected double x;
	protected double y;
	protected boolean canCollideWithWall;
	protected boolean affectedByCollision;
	protected Rect rect;
	protected int screenWidth;
	protected int screenHeight;
	protected double waveCounter; // used for the wave-movement
	protected int movementPattern;
	protected boolean active;
	protected int frameCount;
	protected int currentFrame = 0;
	protected Rect srcRect;
	protected Rect destRect;
	Pair<PhysicalObject,MessageType> pair;

	public PhysicalObject(Bitmap bitmap, double x, double y, int screenWidth,
			int screenHeight, int frameCount) {
		Log.d(TAG, "PO constructor");
		this.bitmap = bitmap;
		this.x = x;
		this.y = y;
		this.screenWidth = screenWidth;
		this.screenHeight = screenHeight;
		spriteWidth = bitmap.getWidth() / frameCount;
		spriteHeight = bitmap.getHeight();
		affectedByCollision = false;
		rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		motion = new Motion();
		waveCounter = Math.random() * Math.PI;
		movementPattern = 0;
		active = false;
		this.frameCount = frameCount;
		spriteHeight = bitmap.getHeight();
		spriteWidth = bitmap.getWidth() / frameCount;

	}

	/*
	 * MovementPatterns (REMOVE?)
	 */
	/*
	 * Straight movement, moving in one direction at constant speed
	 */
	public void moveStraight() {
		motion.setHorizontalAcceleration(0);
		motion.setVerticalAcceleration(0);
		motion.setHorizontalVelocity(1);
		motion.setVerticalVelocity(0); // Settes til + eller -
	}

	/*
	 * Stationary movement, the same speed and direction as the pig
	 */
	public void moveStationary() {
		motion.setHorizontalAcceleration(0);
		motion.setVerticalAcceleration(0);
		motion.setHorizontalVelocity(1);
		motion.setVerticalVelocity(0);

	}

	/*
	 * Circle movement, NOT IMPLEMENTED
	 */
	public void moveCircle() {
	}

	/*
	 * Wave movement, moving the object in a mathematical mave pattern
	 */
	public void moveWave() {
		motion.setHorizontalAcceleration(0);
		motion.setVerticalAcceleration(0);
		motion.setHorizontalVelocity(1);
		motion.setVerticalVelocity(0);
	}

	public abstract void update();

	public abstract void draw(Canvas canvas);

	// GETTERS AND SETTERS:

	public Motion getMotion() {
		return motion;
	}

	public void setMotion(Motion motion) {
		this.motion = motion;
	}

	public double getHorizontalPosition() {
		return x;
	}

	public void setHorizontalPosition(float x) {
		this.x = x;
	}

	public double getVerticalPosition() {
		return y;
	}

	public void setVerticalPosition(float y) {
		this.y = y;
	}

	public int getSpriteWidth() {
		return spriteWidth;
	}

	public int getSpriteHeight() {
		return spriteHeight;
	}

	public boolean getCanCollideWithWall() {
		return canCollideWithWall;
	}

	public Rect getRect() {
		return rect;
	}

	public boolean isAffectedByCollision() {
		return affectedByCollision;
	}

	public Bitmap getBitmap() {
		return bitmap;
	}

	public int getMovementPattern() {
		return movementPattern;
	}

	public void setMovementPattern(int movementPattern) {
		this.movementPattern = movementPattern;
	}

	public void setSpriteWidth(int spriteWidth) {
		this.spriteWidth = spriteWidth;
	}

	public void setSpriteHeight(int spriteHeight) {
		this.spriteHeight = spriteHeight;
	}

	public int getScreenWidth() {
		return screenWidth;
	}

	public void setScreenWidth(int screenWidth) {
		this.screenWidth = screenWidth;
	}

	public int getScreenHeight() {
		return screenHeight;
	}

	public void setScreenHeight(int screenHeight) {
		this.screenHeight = screenHeight;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean b) {
		active = b;
		if(!b){
			setChanged();
			Pair<PhysicalObject,MessageType> pair = new Pair<PhysicalObject, MessageType>(this, MessageType.OUTSIDEVIEW);
			notifyObservers(pair);
		}
	}

	

	

	

}
